'use strict';

/**
*  房间
*/

module.exports = Room;

var uids = [];

function isEmptyObject(obj) {
    for (var k in obj)
        return false;
    return true;
}

// 根据key获取玩家信息
Room.prototype.getUserByKey = function (key, value) {
    for(var k in users){
        var user = users[k];
        if(user[key] === value){
            return user;
        }
    }
    return null;
};

//用于room的声明
function Room(opts) {
    this.roomid = opts.roomid;
    this.users = {};
}

Room.prototype.AddUser = function (user) {
    if (isEmptyObject(user)){
        return;
    }
    
    for (var k in users){
        if (users[k].uid === user.uid){
            return;
        }
    }

    users[user.uid] = user;
    uids.push(user.sid);
};

Room.prototype.GetUserByName = function (nickname) {
    return this.getUserByKey('nickname', nickname);
};

Room.prototype.GetUserNumber = function () {
    var number = 0;
    for (var k in users){
        number += 1;
    }
    return nember;
};

Room.prototype.notifyMsgEnterRoom = function(channel, msg){
    if(!msg){
        logger.error('Can not send empty message enter room');
        return;
    }
    if(!channel){
        logger.error('Can not send empty message enter room');
        return;
    }

    //有没有用待定
    channel.pushMessage('onEntryRoom', msg, uids);
};